package com.jp.knowledge.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.canyinghao.canrefresh.CanRefreshLayout;
import com.google.gson.JsonObject;
import com.jiayen.cache.FileCacheManager;
import com.jiayen.util.ToasUtil;
import com.jp.knowledge.R;
import com.jp.knowledge.activity.BaseFragment;
import com.jp.knowledge.adapter.AttentionDetailAdpter;
import com.jp.knowledge.http.HttpHelper;
import com.jp.knowledge.model.HeadLineData;
import com.jp.knowledge.model.IModel;
import com.jp.knowledge.util.L;
import com.jp.knowledge.util.ResultSubscriber;

import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import rx.Observable;
import rx.Observer;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by jiayen on 16/10/27.
 * 关注详情
 */
public class AttentionDetailFragment extends BaseFragment implements ResultSubscriber.OnResultListener {

    private final int GET_DETAIL_LIST_CODE=0x00000001;//获取详情列表

    @ViewInject(R.id.m_refresh)
    private CanRefreshLayout mRefresh;

    @ViewInject(R.id.can_content_view)
    private RecyclerView recyclerView;

    private View mView;

    private AttentionDetailAdpter adpter;
    private List<HeadLineData>  list;

    private String label;
    private int type;
    private String infoId;

    private FileCacheManager cacheManager;

    private String CACHE_DATA_PATH;//头条的缓存路径这里暂时使用 因为还没有数据联调

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.attention_detail_fragment, null);
        x.view().inject(this, mView);
        initData();
        return mView;
    }

    public void setInfoId(String infoId) {
        this.infoId=infoId;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public void setType(int type) {
        this.type = type;
    }

    private void initData() {
        cacheManager=new FileCacheManager();
        mRefresh.setLoadMoreEnabled(true);
        mRefresh.setRefreshEnabled(false);

        LinearLayoutManager manager = new LinearLayoutManager(mContext,LinearLayoutManager.VERTICAL,false);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(manager);
        list = new ArrayList<>();
        adpter = new AttentionDetailAdpter(mContext,list);
        recyclerView.setAdapter(adpter);


        CACHE_DATA_PATH = getContext().getCacheDir().getPath()+"/head_data_"+label+".data";
//        getData(0);

        //临时读取头条的缓存来使用
        tempReadToutiaoCache();
    }

    /**
     * 临时读取头条的缓存来使用
     */
    private void tempReadToutiaoCache() {

        Observable.create(new Observable.OnSubscribe<List<HeadLineData>>() {

            @Override
            public void call(Subscriber<? super List<HeadLineData>> subscriber) {
                List<HeadLineData> list = (List<HeadLineData>) cacheManager.readObject(CACHE_DATA_PATH);

                subscriber.onNext(list);
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer< List<HeadLineData>>() {
                    @Override
                    public void onCompleted() {

                    }
                    @Override
                    public void onError(Throwable e) {
//                        autoRefresh();//刷新请求网络数据
                    }
                    @Override
                    public void onNext( List<HeadLineData> data ) {
                        list=data;
                        adpter.updataAdapter(list);
//                        headLineDatas=list;
//                        adapter.updataAdapter(list);
//                        autoRefresh();//刷新请求网络数据
                        L.e("缓存数据为======="+list.toString());
                    }
                });
    }

    /**
     * 获取数据
     */
    private void getData(int mode) {
        int time =0;

        JsonObject param = new JsonObject();
        param.addProperty("label",label);
        param.addProperty("time",time);
        param.addProperty("mode",mode);
        param.addProperty("type",type);
        param.addProperty("id",infoId);
        HttpHelper.getInstance(mContext).subscribeDetailsList(param,GET_DETAIL_LIST_CODE,this);
    }

    @Override
    public void onStart(int requestType) {

    }

    @Override
    public void onCompleted(int requestType) {

    }

    @Override
    public void onError(int requestType) {

    }

    @Override
    public void onNext(IModel data, int requestType) {
        int code = data.getErrcode();
        if(code==0){
            List<HeadLineData>  list = data.getList(HeadLineData.class);
            ToasUtil.toast(mContext,list.toString());
            L.e("attentiondetail_fragment="+list.toString());
            adpter.updataAdapter(list);
        }
    }
}
